In [1]:
import pandas as pd
import numpy as np
In [2]:
def cut_and_agg(path):
    df = pd.read_csv(path)
    df['bin'] = pd.cut(df.timestamp, [i for i in np.arange(0, 240.25, 0.25)])
    df = df.groupby(['bin', 'particle_no']).agg({
        'x':['mean'],
        'y':['mean'],
        'z':['mean']})
    df.columns = ['x', 'y', 'z']
    return df.reset_index()
In [3]:
files = ["adaptiverk4-test1.csv",
         "adaptiverk4-test2.csv",
         "adaptiverk4-test3.csv",
         "heun-test1.csv",
         "heun-test2.csv",
         "heun-test3.csv",
         "rk4-test1.csv",
         "rk4-test2.csv",
         "rk4-test3.csv",
         "rk5-test1.csv",
         "rk5-test2.csv",
         "rk5-test3.csv"
         ]
In [4]:
for file in files:
    exec(f"{file.split('.')[0].replace('-', '_')} = cut_and_agg('{file}')")
In [5]:
adaptiverk4_test1
Out[5]:
bin particle_no x y z
0 (0.0, 0.25] 0 -4.000000e+00 4.000000 0
1 (0.0, 0.25] 1 -3.000000e+00 3.997839 0
2 (0.0, 0.25] 2 -2.000000e+00 3.997837 0
3 (0.0, 0.25] 3 -1.000000e+00 3.997837 0
4 (0.0, 0.25] 4 -4.052738e-08 3.997837 0
... ... ... ... ... ...
61435 (239.75, 240.0] 59 -9.638859e-01 -4.901562 0
61436 (239.75, 240.0] 60 6.964504e-02 -4.896837 0
61437 (239.75, 240.0] 61 1.082531e+00 -4.703087 0
61438 (239.75, 240.0] 62 2.049363e+00 -4.390655 0
61439 (239.75, 240.0] 63 2.995046e+00 -4.058573 0

61440 rows × 5 columns

In [6]:
# def calc_distance(x1,y1,z1,x2,y2,z2):
#     dx = x2-x1
#     dy = y2-y1
#     dz = z2-z1
#     return ((dx)**2+ (dy)**2 + (dz)**2)**(0.5)
In [7]:
def calc_error(df1, df2):
    df1_copy = df1.copy()
    df2_copy = df2.copy()
    df2_copy.columns = [str(i)+"2" for i in df2_copy.columns]
    df = pd.concat([df1_copy, df2_copy], axis=1)
    df['error'] = df.apply(lambda row: np.linalg.norm(np.array([row.x, row.y, row.z])-np.array([row.x2, row.y2, row.z2])), axis=1)
    df = df.groupby('bin').agg({
        "error": "mean"}).reset_index()
    df = df[['bin', 'error']]
    return df
In [10]:
for file in (files):
    exec(f"{file.split('.')[0].replace('-', '_') + '_error'} = calc_error({file.split('.')[0].replace('-', '_')}, {'rk5_' + file.split('.')[0].split('-')[1]})")
    print(file.split('.')[0].replace('-', '_') + '_error')
adaptiverk4_test1_error
adaptiverk4_test2_error
adaptiverk4_test3_error
heun_test1_error
heun_test2_error
heun_test3_error
rk4_test1_error
rk4_test2_error
rk4_test3_error
rk5_test1_error
rk5_test2_error
rk5_test3_error
In [11]:
adaptiverk4_test1_error
Out[11]:
bin error
0 (0.0, 0.25] 4.380328e-11
1 (0.25, 0.5] 4.052426e-10
2 (0.5, 0.75] 1.271275e-08
3 (0.75, 1.0] 3.085828e-09
4 (1.0, 1.25] 1.151678e-08
... ... ...
955 (238.75, 239.0] 3.798489e-07
956 (239.0, 239.25] 4.111348e-07
957 (239.25, 239.5] 3.644810e-07
958 (239.5, 239.75] 3.594737e-07
959 (239.75, 240.0] 2.858367e-07

960 rows × 2 columns

In [12]:
import seaborn as sns
In [13]:
errors = [
    "adaptiverk4_test1_error",
    "adaptiverk4_test2_error",
    "adaptiverk4_test3_error",
    "heun_test1_error",
    "heun_test2_error",
    "heun_test3_error",
    "rk4_test1_error",
    "rk4_test2_error",
    "rk4_test3_error"]
In [14]:
for error in errors:
    exec(f"{error}['x']={error}.apply(lambda row: str(row.name), axis=1)")
    print(f"sns.lineplot(data={error}, x='x', y='error')")
sns.lineplot(data=adaptiverk4_test1_error, x='x', y='error')
sns.lineplot(data=adaptiverk4_test2_error, x='x', y='error')
sns.lineplot(data=adaptiverk4_test3_error, x='x', y='error')
sns.lineplot(data=heun_test1_error, x='x', y='error')
sns.lineplot(data=heun_test2_error, x='x', y='error')
sns.lineplot(data=heun_test3_error, x='x', y='error')
sns.lineplot(data=rk4_test1_error, x='x', y='error')
sns.lineplot(data=rk4_test2_error, x='x', y='error')
sns.lineplot(data=rk4_test3_error, x='x', y='error')
In [26]:
sns.lineplot(data=adaptiverk4_test1_error, x='x', y='error')
Out[26]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:54:07.622088 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [16]:
sns.lineplot(data=adaptiverk4_test2_error, x='x', y='error')
Out[16]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:51:08.858361 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [17]:
sns.lineplot(data=adaptiverk4_test3_error, x='x', y='error')
Out[17]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:51:27.874348 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [18]:
sns.lineplot(data=heun_test1_error, x='x', y='error')
Out[18]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:51:44.937568 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [19]:
sns.lineplot(data=heun_test2_error, x='x', y='error')
Out[19]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:52:01.428761 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [20]:
sns.lineplot(data=heun_test2_error, x='x', y='error')
Out[20]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:52:16.918250 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [21]:
sns.lineplot(data=heun_test3_error, x='x', y='error')
Out[21]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:52:36.554289 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [22]:
sns.lineplot(data=rk4_test1_error, x='x', y='error')
Out[22]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:52:56.675952 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [23]:
sns.lineplot(data=rk4_test2_error, x='x', y='error')
Out[23]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:53:17.373069 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [24]:
sns.lineplot(data=rk4_test3_error, x='x', y='error')
Out[24]:
<AxesSubplot:xlabel='x', ylabel='error'>
2021-01-31T23:53:37.370449 image/svg+xml Matplotlib v3.3.2, https://matplotlib.org/
In [ ]: